package com.opensky.module.sys.interfaces.validator;

import com.alibaba.excel.util.CollectionUtils;
import com.opensky.module.sys.infrastructure.exception.MessageCode;
import com.opensky.module.sys.interfaces.assembler.SysUserAssembler;
import com.opensky.module.sys.interfaces.frm.SysUserQueryFrm;
import com.opensky.module.sys.interfaces.frm.SysUserSaveFrm;
import com.opensky.module.sys.interfaces.vo.SysUserVO;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * 用户表(表sys_user)数据校验对象
 *
 * @author 中国印
 * @date 2021-11-26 14:24:35
 */
public class SysUserValidator {

    /**
     * 根据实际业务校验保存参数
     *
     * @param frm 待校验的frm对象
     */
    public static void validateFrm(SysUserAssembler assembler, SysUserSaveFrm frm) {
        String id = frm.getId();
        if (Objects.equals(id, null)) {
            // 新增数据校验
            validateFrmForInsert(assembler, frm);
        } else {
            // 更新数据校验
            validateFrmForUpdate(assembler, frm);
        }
    }

    /**
     * 方法说明：新增对象做校验
     *
     * @param assembler 转换器
     * @param frm       请求参数
     */
    private static void validateFrmForInsert(SysUserAssembler assembler, SysUserSaveFrm frm) {
        String loginName = frm.getLoginName();
        String phone = frm.getPhone();
        String email = frm.getEmail();
        if (StringUtils.isNotBlank(loginName)) {
            List<SysUserVO> loginNameList = assembler.getList(SysUserQueryFrm.builder().loginName(loginName).build());
            if (!CollectionUtils.isEmpty(loginNameList)) {
                throw MessageCode.USER_NAME_REPEAT_ERROR.toException();
            }
        }
        if (StringUtils.isNotBlank(phone)) {
            List<SysUserVO> phoneList = assembler.getList(SysUserQueryFrm.builder().phone(phone).build());
            if (!CollectionUtils.isEmpty(phoneList)) {
                throw MessageCode.USER_PHONE_REPEAT_ERROR.toException();
            }
        }
        if (StringUtils.isNotBlank(email)) {
            List<SysUserVO> emailList = assembler.getList(SysUserQueryFrm.builder().email(email).build());
            if (!CollectionUtils.isEmpty(emailList)) {
                throw MessageCode.USER_EMAIL_REPEAT_ERROR.toException();
            }
        }
    }

    /**
     * 方法说明：更新对象做校验
     *
     * @param assembler 转换器
     * @param frm       请求参数
     */
    private static void validateFrmForUpdate(SysUserAssembler assembler, SysUserSaveFrm frm) {
        String loginName = frm.getLoginName();
        String phone = frm.getPhone();
        String email = frm.getEmail();
        if (StringUtils.isNotBlank(loginName)) {
            List<SysUserVO> loginNameList = assembler.getList(SysUserQueryFrm.builder().loginName(loginName).build());
            if (!CollectionUtils.isEmpty(loginNameList)) {
                loginNameList.forEach(vo -> {
                    if (!Objects.equals(vo.getId(), frm.getId())) {
                        throw MessageCode.USER_NAME_REPEAT_ERROR.toException();
                    }
                });
            }
        }
        if (StringUtils.isNotBlank(phone)) {
            List<SysUserVO> phoneList = assembler.getList(SysUserQueryFrm.builder().phone(phone).build());
            if (!CollectionUtils.isEmpty(phoneList)) {
                phoneList.forEach(vo -> {
                    if (!Objects.equals(vo.getId(), frm.getId())) {
                        throw MessageCode.USER_PHONE_REPEAT_ERROR.toException();
                    }
                });
            }
        }
        if (StringUtils.isNotBlank(email)) {
            List<SysUserVO> emailList = assembler.getList(SysUserQueryFrm.builder().email(email).build());
            if (!CollectionUtils.isEmpty(emailList)) {
                emailList.forEach(vo -> {
                    if (!Objects.equals(vo.getId(), frm.getId())) {
                        throw MessageCode.USER_EMAIL_REPEAT_ERROR.toException();
                    }
                });
            }
        }
    }

}

